package com.example.teaching_server.dto;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.FutureOrPresent;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Positive;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDate;

/**
 * 重新排课请求DTO
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(description = "重新排课请求参数")
public class RescheduleRequest {

    @NotNull(message = "特定日期不能为空")
    @FutureOrPresent(message = "特定日期不能是过去日期")
    @JsonFormat(pattern = "yyyy-MM-dd")
    @Schema(description = "重新排课的特定日期", example = "2024-03-15", requiredMode = Schema.RequiredMode.REQUIRED)
    private LocalDate specificDate;

    @NotNull(message = "时间节次ID不能为空")
    @Positive(message = "时间节次ID必须是正整数")
    @Schema(description = "时间节次ID", example = "3", requiredMode = Schema.RequiredMode.REQUIRED)
    private Long timeSlotId;

    @Schema(description = "教师ID（可选，用于教师调课）", example = "45")
    private Long teacherId;

    @Schema(description = "教室ID（可选，用于场地调整）", example = "101")
    private Long classroomId;

    @Schema(description = "调整原因", example = "教师请假")
    private String reason;

    @Schema(description = "是否通知相关人员", example = "true")
    @Builder.Default
    private Boolean notifyUsers = true;

    /**
     * 验证日期是否为工作日（基础验证）
     */
    @Schema(description = "是否为工作日", accessMode = Schema.AccessMode.READ_ONLY)
    public boolean isWeekday() {
        if (specificDate == null) return false;
        java.time.DayOfWeek dayOfWeek = specificDate.getDayOfWeek();
        return dayOfWeek != java.time.DayOfWeek.SATURDAY &&
                dayOfWeek != java.time.DayOfWeek.SUNDAY;
    }

    /**
     * 获取日期星期几
     */
    @Schema(description = "星期几", accessMode = Schema.AccessMode.READ_ONLY)
    public String getDayOfWeek() {
        if (specificDate == null) return "未知";

        return switch (specificDate.getDayOfWeek()) {
            case MONDAY -> "星期一";
            case TUESDAY -> "星期二";
            case WEDNESDAY -> "星期三";
            case THURSDAY -> "星期四";
            case FRIDAY -> "星期五";
            case SATURDAY -> "星期六";
            case SUNDAY -> "星期日";
        };
    }

    /**
     * 验证请求是否有效
     */
    @Schema(description = "请求是否有效", accessMode = Schema.AccessMode.READ_ONLY)
    public boolean isValidRequest() {
        return specificDate != null && timeSlotId != null && timeSlotId > 0;
    }

    /**
     * 获取请求摘要
     */
    @Schema(description = "请求摘要", accessMode = Schema.AccessMode.READ_ONLY)
    public String getRequestSummary() {
        StringBuilder summary = new StringBuilder();
        if (specificDate != null) {
            summary.append("日期：").append(specificDate).append("（").append(getDayOfWeek()).append("）");
        }
        if (timeSlotId != null) {
            summary.append("，时间节次：").append(timeSlotId);
        }
        if (reason != null && !reason.trim().isEmpty()) {
            summary.append("，原因：").append(reason);
        }
        return summary.toString();
    }
}
